/* eslint-disable @typescript-eslint/no-namespace */
namespace ui {
	enum TweenMode {
		None,
		MoveDown,
		ScaleAdd,
		ScaleDec,
	}

	// bug，点中按钮的上边缘的时候，因为松开的时候已经不在按钮上了，导致点不中！
	export class ButtonEx extends eui.Button implements eui.UIComponent {
		private osx: number;

		private osy: number;

		private ox: number;

		private oy: number;

		mode: TweenMode = TweenMode.ScaleAdd;

		red: eui.Image;

		private data: any;

		// protected partAdded(partName: string, instance: any): void {
		//     super.partAdded(partName, instance);
		// }

		protected override childrenCreated(): void {
			super.childrenCreated();
			this.record();
			// this.anchorOffsetX = this.width / 2;
			// this.anchorOffsetY = this.height / 2;
		}

		record(): void {
			this.ox = this.x;
			this.oy = this.y;
			this.osx = this.scaleX;
			this.osy = this.scaleY;
		}

		restore(): void {
			this.x = this.ox;
			this.y = this.oy;
			this.scaleX = this.osx;
			this.scaleY = this.osy;
		}

		protected override onTouchCancle(event: egret.TouchEvent): void {
			super.onTouchCancle(event);
			// console.log("onTouchCancle",event)
			this.tweenEnd();
		}

		protected onTouchReleaseOutside(event: egret.TouchEvent): void {
			// console.log("onTouchReleaseOutside",event)
			const x = event.$stageX;
			const y = event.$stageY;

			const ox = this.x;
			const oy = this.y;
			const osx = this.scaleX;
			const osy = this.scaleY;
			this.restore();

			const pt = this.globalToLocal(x, y);
			this.x = ox;
			this.y = oy;
			this.scaleX = osx;
			this.scaleY = osy;
			// if (this.inOriginButton(pt.x,pt.y))
			if (pt.x >= 0 && pt.x < this.width && pt.y >= 0 && pt.y <= this.height) {
				console.log('onTouchReleaseOutside but really in');
				this.dispatchEventWith(egret.TouchEvent.TOUCH_TAP);
			}

			this.tweenEnd();
		}

		protected tweenEnd(): void {
			const tween: egret.Tween = egret.Tween.get(this);
			// tween.to({ scaleX: this.osx, scaleY: this.osy }, 60)
			switch (this.mode) {
				case TweenMode.MoveDown: {
					tween.to({ y: this.oy, x: this.ox }, 10);
					break;
				}
				case TweenMode.ScaleDec:
				case TweenMode.ScaleAdd: {
					tween.to({ scaleX: this.osx, scaleY: this.osy }, 60);
					break;
				}
				default:
			}
		}

		protected override onTouchBegin(event: egret.TouchEvent): void {
			super.onTouchBegin(event);

			const tween: egret.Tween = egret.Tween.get(this);
			switch (this.mode) {
				case TweenMode.MoveDown: {
					tween.to({ y: this.oy + 5, x: this.ox + 2 }, 60);
					break;
				}
				case TweenMode.ScaleDec: {
					tween.to({ scaleX: 1.1 * this.osx, scaleY: 1.1 * this.osy }, 60);
					break;
				}
				case TweenMode.ScaleAdd: {
					tween.to({ scaleX: 0.9 * this.osx, scaleY: 0.9 * this.osy }, 60);
					break;
				}
				default:
			}
			// tween.to({ scaleX: 0.9 * this.osx, scaleY: 0.9 * this.osy }, 60)

			this.addEventListener('touchReleaseOutside', this.onTouchReleaseOutside, this);
			music.playOnce('skin/click.wav',1);
		}

		protected override buttonReleased(): void {
			super.buttonReleased();

			this.tweenEnd();
		}

		showRed(show: boolean): void {
			this.red.visible = show;
		}

		/**
		 * 禁用按钮，同时不再响应用户交互
		 */
		disable(): void {
			const filter = getFilterStyle('置灰');
			this.filters = [filter];
			this.enabled = false;
		}

		/**
		 * 激活按钮，响应用户交互
		 */
		enable(): void {
			this.filters = [];
			this.enabled = true;
		}

		/**
		 * 仅置灰按钮，不改变用户交互状态
		 */
		grayStyle(): void {
			const filter = getFilterStyle('置灰');
			this.filters = [filter];
		}
	}
}
